package com.android.RssReader.Adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.RssReader.R;
import com.android.RssReader.Model.NewsInfoBean;
import com.android.RssReader.Screen.ScreenArgs;

public class NewsBaseAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<NewsInfoBean> list;

	public NewsBaseAdapter(Context context, List<NewsInfoBean> list) {
		this.list = list;
		this.inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}
	
	
	

	public void setList(List<NewsInfoBean> list){
		this.list = list;
		this.notifyDataSetChanged();
	}
	
	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		NewsItem news ;
		if (convertView == null) {
			View view  = inflater.inflate(R.layout.screen_home_news_item, null);
			news = new NewsItem();
			news.title=(TextView) view.findViewById(R.id.news_item_title_tv);
			news.description = (TextView) view.findViewById(R.id.news_item_description_tv);
			news.source=(TextView) view.findViewById(R.id.news_item_source_tv);
			news.date = (TextView) view.findViewById(R.id.news_item_time_tv);
			view.setTag(R.layout.screen_home_news_item, news);
			convertView = view;
		} else {
			news = (NewsItem) convertView.getTag(R.layout.screen_home_news_item);
		}
		final NewsInfoBean data = list.get(position);
		if (data != null) {
			news.title.setText(data.getTitle());
			news.description.setText(data.getDescription());
			news.source.setText(data.getSource());
			news.date.setText(data.getPubDate());
			news.link = data.getLink();
		}
		ScreenArgs args = new ScreenArgs();
		args.putExtra("title", data.getTitle());
		args.putExtra("description", data.getDescription());
		args.putExtra("source", data.getSource());
		args.putExtra("date", data.getPubDate());
		args.putExtra("link", data.getLink());
		convertView.setTag(args);
		return convertView;
	}

	private class NewsItem {
		private TextView title;
		private TextView description;
		private TextView source;
		private TextView date;
		private String link;
	}
	
}
